package tony.com.xframe.position.gdposition;

import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;

import tony.com.xframe.BaseApplication;

/**
 * Created by Tony on 2016-01-05.
 */
public class GDPosition {
    //声明AMapLocationClient类对象
    private AMapLocationClient mLocationClient = null;
    //声明mLocationOption对象
    private AMapLocationClientOption mLocationOption = null;
    private static GDPosition gdPosition;


    public static GDPosition getInstance() {
        if (gdPosition == null) {
            gdPosition = new GDPosition();
        }
        return gdPosition;
    }

    public GDPosition() {
//        initMap();
    }

    AMapLocationListener mLocationListener;


    /**
     * 初始化地图的基本数据
     */
    private void initMap() {
        //初始化定位
        mLocationClient = BaseApplication.mLocationClient;
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);

        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数

        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
    }

    public void getPosition(final GDPositionListener gdPositionListener){

         mLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                Log.e("info",aMapLocation+"");
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        mLocationClient.stopLocation();//当获取到定位信息以后，停止更新
//                        this.onLocationChanged(aMapLocation);
                        gdPositionListener.getPositionResult(aMapLocation);
                    }
                }
            }
        };
        initMap();
    }

    public interface GDPositionListener {
        AMapLocation getPositionResult(AMapLocation aMapLocation);
    }


}
